package sg.edu.nus.iss.vmcs.payment;

import java.awt.*;

import sg.edu.nus.iss.vmcs.store.Coin;
import sg.edu.nus.iss.vmcs.store.CashStore;
import sg.edu.nus.iss.vmcs.store.Store;
import sg.edu.nus.iss.vmcs.store.StoreItem;
import sg.edu.nus.iss.vmcs.store.StoreObject;

import sg.edu.nus.iss.vmcs.store.StoreController;

public class CoinInputBox extends Panel {
	private static final long serialVersionUID = 8371731484495760471L;

	private double coin_weights[];

	private CoinReceiver ctrl;
	private StoreController storeCtrl;
	private int len;
	private Button[] buttons;
	private Button invalidCoinButton;
	private Button problemCoinInputButton;
	private CoinInputBoxListener coinInputBoxListener = null;
	
	public CoinInputBoxListener getListener(){
		if (coinInputBoxListener ==null){
			coinInputBoxListener = new CoinInputBoxListener(ctrl);
		}
		return coinInputBoxListener;
	}

	public CoinInputBox(Panel composite, CoinReceiver controller) {
		ctrl = controller;
		storeCtrl = ctrl.getStoreController();
		coinInputBoxListener = new CoinInputBoxListener(ctrl);

		len = storeCtrl.getStoreSize(Store.CASH);
		StoreItem[] items = storeCtrl.getStoreItems(Store.CASH);

		this.setLayout(new GridLayout(0, 1));

		// add title
		Label lb = new Label("Enter Coins Here", Label.LEFT);
		this.add(lb);

		// add coin buttons
		Panel coinPanel = new Panel();
		coinPanel.setLayout(new GridLayout(0, len + 1)); // the +1 is for additional space
															// for Invalid Coin
		int i;
		StoreObject coin;
		buttons = new Button[len];
		coin_weights = new double[len];
//		CoinInputBoxListener cibl = new CoinInputBoxListener();
		for (i = 0; i < len; i++) {
			coin = items[i].getContent();
			coin_weights[i] = ((Coin) coin).getWeight();
			buttons[i] = new Button(coin.getName());
			buttons[i].addActionListener(getListener());
			buttons[i].setActionCommand(String.valueOf(coin_weights[i]));
			buttons[i].setEnabled(false);
			coinPanel.add(buttons[i]);
		}
		invalidCoinButton = new Button("Invalid");
		invalidCoinButton.addActionListener(getListener());
		invalidCoinButton.setActionCommand(String
				.valueOf(CashStore.INVALID_COIN_WEIGHT));
		invalidCoinButton.setEnabled(false);
		coinPanel.add(invalidCoinButton);

		problemCoinInputButton = new Button("Clear Coin Receiver Error");
		problemCoinInputButton.addActionListener(getListener());
		problemCoinInputButton.setActionCommand(CashStore.COIN_RECEIVER_CLEAR_ERR);
		problemCoinInputButton.setEnabled(true);
		setProblemButton(false);
		coinPanel.add(problemCoinInputButton);	
		
		this.add(coinPanel);		
	}

	public void setActive(boolean b) {
		for (int i = 0; i < len; i++) {
			buttons[i].setEnabled(b);
		}
		invalidCoinButton.setEnabled(b);
	}
	
	/**
	 * Set the Problem Button.
	 * @param err
	 * If err is 'True', it indicate as currently Coin Receiver has error
	 */
	public void setProblemButton(boolean err){
		if (err){
			problemCoinInputButton.setLabel("Clear Coin Receiver Error");
			problemCoinInputButton.setActionCommand(CashStore.COIN_RECEIVER_CLEAR_ERR);
		}else{
			problemCoinInputButton.setLabel("Set Coin Receiver Error");
			problemCoinInputButton.setActionCommand(CashStore.COIN_RECEIVER_ERR);
		}
	}
	
		
}
